package com.example.node.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.node.R;
import com.example.node.entity.Constant;
import com.example.node.listener.OnOkHttpCallback;
import com.example.node.model.WallpaperBingModel;
import com.example.node.model.WallpaperClassModel;
import com.example.node.utils.GlideUtils;
import com.example.node.utils.OkHttpUtils;
import com.example.node.view.AutoViewPager;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;

public class WallpaperFragment extends Fragment {
    private LinearLayout mLoadingLayout;
    private AutoViewPager mAutoViewPager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.wallpaper_fragment,container,false);

        initView(view);
        return view;
    }

    private void initView(View view) {

        mLoadingLayout=view.findViewById(R.id.mLoadingLayout);
        mAutoViewPager=view.findViewById(R.id.mAutoViewPager);
        mAutoViewPager.setAutoTime(3*1000);
        mAutoViewPager.setStartAuto(true);

        //初始化轮播图
        initSpecial();
        //初始化横向分类
        //initCategory();
        //每天壁纸推荐
        //initEveryday();
    }

    //初始化轮播图
    private void initSpecial() {
        OkHttpUtils.get(Constant.WALLPAPER_BING, new OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String meg) {

            }

            @Override
            public void onSuccessful(String json) {
                if(!TextUtils.isEmpty(json)){
                    parsingBingJson(json);
                }
            }
        });
    }

    private void parsingBingJson(String json) {
        WallpaperBingModel wallpaperBingModel=new Gson().fromJson(json, WallpaperBingModel.class);
        for (int i=0;i<wallpaperBingModel.getImages().size();i++){

        }
    }

    //初始化横向分类
    private void initCategory() {
        OkHttpUtils.get(Constant.WALLPAPER_CLASS_URL, new OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String meg) {

            }

            @Override
            public void onSuccessful(String json) {
                if(!TextUtils.isEmpty(json)){
                    parsingWallpaperJson(json);
                }
            }
        });
    }

    private void parsingWallpaperJson(String json) {
        WallpaperClassModel wallpaperClassModel=new Gson().fromJson(json, WallpaperClassModel.class);
        for (int i=0;i<wallpaperClassModel.getData().size();i++){
            View view=View.inflate(getActivity(),R.layout.layout_special_item,null);
            TextView tv_title=view.findViewById(R.id.tv_title);
            TextView tv_content=view.findViewById(R.id.tv_content);
            ImageView iv_image=view.findViewById(R.id.iv_image);

            tv_title.setText("");
            tv_content.setText("");
            GlideUtils.loadImageView(getActivity(),"",iv_image);
        }
    }

    //每天壁纸推荐
    private void initEveryday() {
        OkHttpUtils.get(Constant.WALLPAPER_NEW_URL, new OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String meg) {

            }

            @Override
            public void onSuccessful(String json) {
                if(!TextUtils.isEmpty(json)){
                    parsingNewJson(json);
                }
            }
        });
    }

    private void parsingNewJson(String json) {
        //隐藏加载动画
        mLoadingLayout.setVisibility(View.GONE);
        try {
            JSONObject jsonObject=new JSONObject(json);
            JSONArray jsonArray=jsonObject.getJSONArray("data");
            if (jsonArray.length()>0){
                for (int i=0;i<jsonArray.length();i++){
                    JSONObject jsonData= (JSONObject) jsonArray.get(i);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            if(mAutoViewPager!=null){
                if (mAutoViewPager.getStartAuto()){
                    mAutoViewPager.setStartAuto(false);
                }
            }
        }else {
            if(mAutoViewPager!=null){
                if (!mAutoViewPager.getStartAuto()){
                    mAutoViewPager.setStartAuto(true);
                }
            }
        }
    }
}
